import Sprite from '../base/sprite'
import DataBus  from '../databus'
import _COIGIG from './mapdata'
const screenWidth  = window.innerWidth
const screenHeight = window.innerHeight

const BG_IMG_SRC   = 'images/bg.jpg'

	//const _COS = [1,0,-1,0]
    //const _SIN = [0,1,0,-1]
	let databus3 = new DataBus()

	

/**
 * 游戏item类
 * 提供update和render函数实现无限滚动的背景功能
 */
export default class Beans extends Sprite {
	
	
  constructor(ctx) {
    super("", 0, 0) //不需背景图片

    this.top = 0

	var index=databus3.GAME_INDEX
	
	
	this.x_length=_COIGIG[index]['map'][0].length
	this.y_length=_COIGIG[index]['map'].length
    console.log(this.x_length)
	console.log(this.y_length)
	
	this.x_length=_COIGIG[index]['map'][0].length
	this.y_length=_COIGIG[index]['map'].length
   
    this.beanmap=null
	//define two dimension map data
	if (this.beanmap==null){
		this.beanmap = new Array();	
		for (var j = 0; j<this.y_length;j++){ //this.mapdata定义在sprite类
		   this.beanmap[j]=new Array();
		}
		
		//取地图数据
	    this.getmapdata(index)
	
	}
	this.times =0;
    var myDate = new Date();
	this.curmill= myDate.getTime(); //毫秒数
	
	this.ebnum=0
    
	
	
    this.render(ctx)
	
	
  }
    
	isempty(mapindex){
	   
	   for (var j = 0; j<this.y_length;j++)
		   for (var i=0;i<this.x_length;i++){
		       if (this.beanmap[j][i]==0){ //还有豆
			      return false;
			   }
			   //console.log(this.mapdata[j][i])
		   }
	   
	   return true;	   
	   
	}
	
    getmapdata(mapindex){
	   var mapi=_COIGIG[mapindex]['map'];
	   //console.log(mapi[0][0]);
	   
	   for (var j = 0; j<this.y_length;j++)
		   for (var i=0;i<this.x_length;i++){
		       this.beanmap[j][i]=mapi[j][i]
			   //console.log(this.mapdata[j][i])
		   }
	}
  
  //获取地图上某点的值
    get(x,y){
        if(this.beanmap[y]&&typeof this.beanmap[y][x]!='undefined'){
            return this.beanmap[y][x];
        }
		//console.log("err")
        return -1;
    }
    
	//设置地图上某点的值
    set(x,y,value){
        if(this.beanmap[y]){
            this.beanmap[y][x] = value;
        }
    }
    
	//画物品
	drawitems (context,mapindex){
		//console.log("draw items begin")
		
		//console.log(_COIGIG[mapindex]['goods']['1,3'])
		
		for(var j=0; j<this.y_length; j++){
			for(var i=0; i<this.x_length; i++){
				
				//if (j==23) //吃豆
				//   continue;
				
				if(!this.get(i,j)){
					var pos = this.coord2position(i,j);
					context.fillStyle = "#F5F5DC";
					if(_COIGIG[mapindex]['goods'][i+','+j]){
						context.beginPath();
						//times=10
						context.arc(pos.x,pos.y,3+this.times%2,0,2*Math.PI,true);
						context.fill();
						context.closePath();
					}else{
						context.fillRect(pos.x-2,pos.y-2,4,4);
					}
				}else{
					//if (j==23)
					 //  console.log("item"+"i="+i+",j="+j)
				}
			}
		}
	}
  
  
    update() {
	  var myDate = new Date();
	  var nowmill= myDate.getTime(); //毫秒数
	
	  if (nowmill-this.curmill>=300){ //0.3 second 豆子变大变小
	    this.times++;
	    this.curmill=nowmill;
		//databus4.MAP_OFFSETY=databus4.MAP_OFFSETY+1
	  }
    }
	

  /**
   * 背景图画地图函数
   */
  render(ctx) {
    
	
    var index=databus3.GAME_INDEX
    
	this.drawitems(ctx,index)
	//}
	
  }
}
